package com.yhos.platform.oopston.core.web;

import com.yhos.platform.oopston.core.annotation.ChannelType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.mvc.condition.RequestCondition;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.lang.reflect.Method;

/**
 * 自定义HandlerMapping
 */
public class ChannelTypeHandlerMapping extends RequestMappingHandlerMapping {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    protected RequestCondition<?> getCustomMethodCondition(Method method) {
        logger.info("---ChannelTypeHandlerMapping.getCustomMethodCondition---");
        ChannelType channelType = method.getAnnotation(ChannelType.class);
        if (channelType != null) {
            //如果有渠道类型注解，则执行自定义路由解析
            return new ChannelTypeRequestCondition(channelType.channels());
        }
        //如果没有渠道类型注解则执行默认路由解析
        return super.getCustomMethodCondition(method);
    }

}
